import { Component, Input, OnInit } from '@angular/core';
import { ApiService } from "@core/services/http/api.service";
import {ApesMessageService} from "@shared/apes/message";
import {DatePipe} from "@angular/common";

@Component({
  selector: 'car-tabs',
  templateUrl: './car-tabs.component.html',
  styleUrls: ['./car-tabs.component.scss']
})
export class CarTabsComponent implements OnInit {
  @Input()
  set parmas(value:any) {
    this.title = value["title"];
    this.titleList = value["titleList"];
    this.steps = value["stepsApi"];
    this._parmas = value;
  }

  get parmas() {
    return this._parmas;
  }

  _parmas:any;
  title:string;
  titleList:any[] = [];
  steps:string;
  itmeData = "0分钟";
  itemList:any[] = [];

  jjcdhNum:any;
  cphm: any;
  mdid: any;
  check:string = '1';
  rowData:any [] = [];
  reportData: any[] = [];
  tablesIndex: number = 0;

  constructor(private apiService:ApiService,
              private msg: ApesMessageService,
              private datePipe: DatePipe) {
    setInterval(() => {
      this.reportData = [];
      this.pageList(this.rowData);
    }, 5000)
  }

  ngOnInit() {
  }

  getFun(key:string, cphm: string, mdid: string) {
    this.jjcdhNum = key;
    this.cphm = cphm;
    this.mdid = mdid;
    this.rowData = [];
    this.itemList = [];
    this.itmeData = '0分钟';
    this.getHead();
    this.getSteps();
    this.getData();
  }

  getHead() {
    if (this.parmas["headApi"]) {
      let data = {'interfaceMethod': this.parmas["headApi"], 'pdata': {'JJCDH': this.jjcdhNum, 'CPHM':this.cphm, 'MDID': this.mdid,'ZT': this.check}};
      this.apiService.post('report.interface', {'body': JSON.stringify(data)}).subscribe(
        (resultData) => {
          const result = JSON.parse(resultData);
          if (result.success) {
            if (result.obj.length !== 0) {
              const data = JSON.parse(result.obj);
              for (let i = 0, len = data.length; i < len; i++) {
                if (data[i].ZHS !== undefined) {
                  this.itmeData = this.toHour(data[i].ZHS);
                } else {
                  this.itmeData = "0分钟";
                }
              }
            }
          } else {
            this.msg.info(result.msg);
          }
        });
    }
  }

  getSteps() {
    if (this.parmas["stepsApi"]) {
      let data = {'interfaceMethod': this.parmas["stepsApi"], 'pdata': {'JJCDH': this.jjcdhNum, 'CPHM':this.cphm, 'MDID': this.mdid,'ZT': this.check}};
      // debugger;
      this.apiService.post('report.interface', {'body': JSON.stringify(data)}).subscribe(
        (resultData) => {
          //debugger;
          const result = JSON.parse(resultData);
          if (result.success) {
            if (result.obj.length !== 0) {
              const data = JSON.parse(result.obj);
              for (let i = 0, len = data.items.length; i < len; i++) {
                data.items[i]['Description'] = this.toHourMinute(data.items[i]['Description']);
              }
              this.itemList.push(data);
            }
          } else {
            this.msg.info(result.msg);
          }
        });
    }
  }

  getData() {
    if (this.parmas["api"]) {
      let data = {'interfaceMethod': this.parmas["api"], 'pdata': {'JJCDH': this.jjcdhNum, 'CPHM':this.cphm, 'MDID': this.mdid,'ZT': this.check}};
      this.apiService.post('report.interface', {'body': JSON.stringify(data)}).subscribe(
        (resultData) => {
          const result = JSON.parse(resultData);
          if (result.success) {
            if (result.obj.length !== 0) {
              const data = JSON.parse(result.obj);
              for (let i = 0, len = data.length; i < len; i++) {
                data[i]['KSRQ'] = this.timesMDHD(data[i]['KSRQ']);
                data[i]['KSRQ_SJ'] = this.timesMDHD(data[i]['KSRQ_SJ']);
                data[i]['SHRQ_SJ'] = this.timesMDHD(data[i]['SHRQ_SJ']);
                data[i]['YQDMDRQ_SJ'] = this.timesMDHD(data[i]['YQDMDRQ_SJ']);
                data[i]['YJDMDRQ_SJ'] = this.timesMDHD(data[i]['YJDMDRQ_SJ']);
                data[i]['SGKSRQ_SJ'] = this.datePipe.transform(
                  data[i]['SGKSRQ_SJ'],'MM-dd HH:mm');
                data[i]['SGJSRQ_SJ'] = this.datePipe.transform(
                  data[i]['SGJSRQ_SJ'],'MM-dd HH:mm');
              }
              if (data.DETAILS == undefined) {
                this.rowData = data;
              } else {
                this.rowData = data.DETAILS;
              }
              this.pageList(this.rowData);
            }
          } else {
            this.msg.info(result.msg);
          }
        });
    }
  }

  //显示时间 天/小时   小时/分钟  分钟
  toHour(minutes) {
    if (Math.floor(minutes / 60 / 24) >= 1) {
        return (Math.floor(minutes / 60 / 24) + '天' + Math.floor(minutes / 60 % 24) + '小时');
    } else if (Math.floor(minutes / 60 / 24) < 1) {
      if (Math.floor(minutes) < 60) {
        return (Math.floor(minutes % 60) + '分钟');
      }
      if(Math.floor(minutes % 60) === 0){
        return (Math.floor(minutes / 60) + '小时')
      }
      return (Math.floor(minutes / 60) + '小时' +  Math.floor(minutes % 60) + '分钟');
    }
  }

  // 只显示 天 小时 分
  toHourMinute(minutes) {
    if (Math.floor(minutes / 60 / 24) >= 1) {
      if (Math.floor(minutes / 60 / 24) < 10) {
        return (Math.floor(minutes / 60 / 24) + "天");
      }
      return (Math.floor(minutes / 60 / 24) + "天");
    } else if (Math.floor(minutes / 60 / 24) < 1) {
      if (Math.floor(minutes) < 60) {
        return (Math.floor(minutes % 60) + "分钟");
      }
      return (Math.floor(minutes / 60) + "小时");
    }
  }

  // 分页显示
  pageList(arrData: any[]) {
    const count = arrData.length;
    this.reportData = [];
    let tempIndex = 0;
    for (let i = this.tablesIndex; i < count; i++) {
      if (tempIndex < 3) {
        this.reportData.push(arrData[i]);
      } else {
        break;
      }
      tempIndex++;
      this.tablesIndex++;
    }
    //当数据遍历完 重新请求数据
    if (this.tablesIndex >= count) {
      this.tablesIndex = 0;
    }
  }

  // 截取时间
  timesMDHD(key:string ) {
    if( key !== undefined ) {
      key = key.slice(5, 16);
    }
    return key;
  }

}
